# File: cdfh.pxd
# Cython translation of cdf.h

cdef extern from "../cdf34_1-dist/linux/src/include/cdf.h":
    ctypedef void *CDFid
    ctypedef long CDFstatus

    double *TT2000NULL = 0

    # Define constants
    enum:
        # * Limits
        CDF_MIN_DIMS  = 0  # Min number of dimensions a CDF variable may have
        CDF_MAX_DIMS  = 10 # Max number of dimensions a CDF variable may have

        # * Lengths
        CDF_VAR_NAME_LEN      = 64
        CDF_ATTR_NAME_LEN     = 64

        CDF_VAR_NAME_LEN256   = 256
        CDF_ATTR_NAME_LEN256  = 256

        CDF_COPYRIGHT_LEN     = 256
        CDF_STATUSTEXT_LEN    = 120
        CDF_PATHNAME_LEN      = 512

        EPOCH_STRING_LEN      = 24
        EPOCH1_STRING_LEN     = 16
        EPOCH2_STRING_LEN     = 14
        EPOCH3_STRING_LEN     = 24
        EPOCH4_STRING_LEN     = 23

        EPOCH16_STRING_LEN      = 36
        EPOCH16_1_STRING_LEN    = 24
        EPOCH16_2_STRING_LEN    = 14
        EPOCH16_3_STRING_LEN    = 36
        EPOCH16_4_STRING_LEN    = 32

        TT2000_0_STRING_LEN     = 30
        TT2000_1_STRING_LEN     = 19
        TT2000_2_STRING_LEN     = 14
        TT2000_3_STRING_LEN     = 29

        EPOCHx_STRING_MAX    = 50
        EPOCHx_FORMAT_MAX    = 68

        # * Data types.
        CDF_INT1        = 1
        CDF_INT2        = 2
        CDF_INT4        = 4
        CDF_INT8        = 8
        CDF_UINT1       = 11
        CDF_UINT2       = 12
        CDF_UINT4       = 14
        CDF_REAL4       = 21
        CDF_REAL8       = 22
        CDF_EPOCH       = 31    # Standard style.
        CDF_EPOCH16     = 32    # Extended style.
        CDF_TIME_TT2000 = 33    # One more style with leap seconds and 
                                # J2000 base time.
        CDF_BYTE        = 41    # same as CDF_INT1 (signed)
        CDF_FLOAT       = 44    # same as CDF_REAL4
        CDF_DOUBLE      = 45    # same as CDF_REAL8
        CDF_CHAR        = 51    # a "string" data type
        CDF_UCHAR       = 52    # a "string" data type

        # * Encoding (for data only, everything else is network encoding).
        NETWORK_ENCODING        = 1
        SUN_ENCODING            = 2
        VAX_ENCODING            = 3
        DECSTATION_ENCODING     = 4
        SGi_ENCODING            = 5
        IBMPC_ENCODING          = 6
        IBMRS_ENCODING          = 7
        HOST_ENCODING           = 8
        PPC_ENCODING            = 9
        HP_ENCODING             = 11
        NeXT_ENCODING           = 12
        ALPHAOSF1_ENCODING      = 13
        ALPHAVMSd_ENCODING      = 14
        ALPHAVMSg_ENCODING      = 15
        ALPHAVMSi_ENCODING      = 16

        # * Decodings.
        NETWORK_DECODING        = NETWORK_ENCODING
        SUN_DECODING            = SUN_ENCODING
        VAX_DECODING            = VAX_ENCODING
        DECSTATION_DECODING     = DECSTATION_ENCODING
        SGi_DECODING            = SGi_ENCODING
        IBMPC_DECODING          = IBMPC_ENCODING
        IBMRS_DECODING          = IBMRS_ENCODING
        HOST_DECODING           = HOST_ENCODING
        PPC_DECODING            = PPC_ENCODING
        MAC_ENCODING            = PPC_ENCODING
        MAC_DECODING            = PPC_ENCODING
        HP_DECODING             = HP_ENCODING
        NeXT_DECODING           = NeXT_ENCODING
        ALPHAOSF1_DECODING      = ALPHAOSF1_ENCODING
        ALPHAVMSd_DECODING      = ALPHAVMSd_ENCODING
        ALPHAVMSg_DECODING      = ALPHAVMSg_ENCODING
        ALPHAVMSi_DECODING      = ALPHAVMSi_ENCODING

        # * Variance flags
        VARY    = -1        # TRUE record or dimension variance flag
        NOVARY  = 0         # FALSE record or dimension variance flag

        # * Majorities
        ROW_MAJOR      = 1
        COLUMN_MAJOR   = 2

        # * Formats.
        SINGLE_FILE    = 1
        MULTI_FILE     = 2

        # * Checksum
        NO_CHECKSUM    = 0
        MD5_CHECKSUM   = 1
        OTHER_CHECKSUM = 2

        # * Attribute scopes
        GLOBAL_SCOPE           = 1
        VARIABLE_SCOPE         = 2

        # * Readonly modes.
        READONLYon             = -1
        READONLYoff            = 0

        # * Validate data modes.
        VALIDATEFILEon         = -1
        VALIDATEFILEoff        = 0

        # * zModes.
        zMODEoff               = 0
        zMODEon1               = 1
        zMODEon2               = 2

        # * Negative to positive floating point zero modes.
        NEGtoPOSfp0on          = -1
        NEGtoPOSfp0off         = 0

        # * Backward file mode. 
        BACKWARDFILEon         = 1
        BACKWARDFILEoff        = 0

        # * Compression/sparseness constants.
        CDF_MAX_PARMS            = 5
        NO_COMPRESSION           = 0
        RLE_COMPRESSION          = 1
        HUFF_COMPRESSION         = 2
        AHUFF_COMPRESSION        = 3
        GZIP_COMPRESSION         = 5
        RLE_OF_ZEROs             = 0
        OPTIMAL_ENCODING_TREES   = 0
        NO_SPARSEARRAYS          = 0
        NO_SPARSERECORDS         = 0
        PAD_SPARSERECORDS        = 1
        PREV_SPARSERECORDS       = 2

# * Invalid/reserved constants.
# #define RESERVED_CDFID      ((CDFid) NULL)      /* Indicates that a CDF hasn't
#                            been selected yet. */
# #define RESERVED_CDFSTATUS  ((CDFstatus) (-1))  /* Indicates that a CDFstatus
#                            hasn't been selected yet. */

#define ILLEGAL_EPOCH_VALUE    (-1.0)
#define ILLEGAL_TT2000_VALUE    (-9223372036854775805LL)
#define FILLED_TT2000_VALUE    (-9223372036854775807LL-1)

        # * Status codes (CDFstatus)
        # *  - informatory codes are greater than CDF_OK
        VIRTUAL_RECORD_DATA             = 1001
        DID_NOT_COMPRESS                = 1002
        VAR_ALREADY_CLOSED              = 1003
        SINGLE_FILE_FORMAT              = 1004
        NO_PADVALUE_SPECIFIED           = 1005
        NO_VARS_IN_CDF                  = 1006
        MULTI_FILE_FORMAT               = 1007
        SOME_ALREADY_ALLOCATED          = 1008
        PRECEEDING_RECORDS_ALLOCATED    = 1009

        CDF_OK                          = 0

        ATTR_NAME_TRUNC                 = -1001
        CDF_NAME_TRUNC                  = -1002
        VAR_NAME_TRUNC                  = -1003
        NEGATIVE_FP_ZERO                = -1004
#                     /* -1005 unused. */
        FORCED_PARAMETER                = -1006
        NA_FOR_VARIABLE                 = -1007

        CDF_WARN                        = -2000

        ATTR_EXISTS                     = -2001
        BAD_CDF_ID                      = -2002
        BAD_DATA_TYPE                   = -2003
        BAD_DIM_SIZE                    = -2004
        BAD_DIM_INDEX                   = -2005
        BAD_ENCODING                    = -2006
        BAD_MAJORITY                    = -2007
        BAD_NUM_DIMS                    = -2008
        BAD_REC_NUM                     = -2009
        BAD_SCOPE                       = -2010
        BAD_NUM_ELEMS                   = -2011
        CDF_OPEN_ERROR                  = -2012
        CDF_EXISTS                      = -2013
        BAD_FORMAT                      = -2014
        BAD_ALLOCATE_RECS               = -2015
        BAD_CDF_EXTENSION               = -2016
        NO_SUCH_ATTR                    = -2017
        NO_SUCH_ENTRY                   = -2018
        NO_SUCH_VAR                     = -2019
        VAR_READ_ERROR                  = -2020
        VAR_WRITE_ERROR                 = -2021
        BAD_ARGUMENT                    = -2022
        IBM_PC_OVERFLOW                 = -2023
        TOO_MANY_VARS                   = -2024
        VAR_EXISTS                      = -2025
        BAD_MALLOC                      = -2026
        NOT_A_CDF                       = -2027
        CORRUPTED_V2_CDF                = -2028
        VAR_OPEN_ERROR                  = -2029
        BAD_INITIAL_RECS                = -2030
        BAD_BLOCKING_FACTOR             = -2031
        END_OF_VAR                      = -2032
#                     /* -2033 unused. */
        BAD_CDFSTATUS                   = -2034
        CDF_INTERNAL_ERROR              = -2035
        BAD_NUM_VARS                    = -2036
        BAD_REC_COUNT                   = -2037
        BAD_REC_INTERVAL                = -2038
        BAD_DIM_COUNT                   = -2039
        BAD_DIM_INTERVAL                = -2040
        BAD_VAR_NUM                     = -2041
        BAD_ATTR_NUM                    = -2042
        BAD_ENTRY_NUM                   = -2043
        BAD_ATTR_NAME                   = -2044
        BAD_VAR_NAME                    = -2045
        NO_ATTR_SELECTED                = -2046
        NO_ENTRY_SELECTED               = -2047
        NO_VAR_SELECTED                 = -2048
        BAD_CDF_NAME                    = -2049
#                     /* -2050 unused. */
        CANNOT_CHANGE                   = -2051
        NO_STATUS_SELECTED              = -2052
        NO_CDF_SELECTED                 = -2053
        READ_ONLY_DISTRIBUTION          = -2054
        CDF_CLOSE_ERROR                 = -2055
        VAR_CLOSE_ERROR                 = -2056
#                     /* -2057 unused. */
        BAD_FNC_OR_ITEM                 = -2058
#                     /* -2059 unused. */
        ILLEGAL_ON_V1_CDF               = -2060
#                     /* -2061 unused. */
#                     /* -2062 unused. */
        BAD_CACHE_SIZE                  = -2063
#                     /* -2064 unused. */
#                     /* -2065 unused. */
        CDF_CREATE_ERROR                = -2066
        NO_SUCH_CDF                     = -2067
        VAR_CREATE_ERROR                = -2068
#                     /* -2069 unused. */
        READ_ONLY_MODE                  = -2070
        ILLEGAL_IN_zMODE                = -2071
        BAD_zMODE                       = -2072
        BAD_READONLY_MODE               = -2073
        CDF_READ_ERROR                  = -2074
        CDF_WRITE_ERROR                 = -2075
        ILLEGAL_FOR_SCOPE               = -2076
        NO_MORE_ACCESS                  = -2077
#                     /* -2078 unused. */
        BAD_DECODING                    = -2079
#                     /* -2080 unused. */
        BAD_NEGtoPOSfp0_MODE            = -2081
        UNSUPPORTED_OPERATION           = -2082
        CDF_SAVE_ERROR                  = -2083
        VAR_SAVE_ERROR                  = -2084
#                     /* -2085 unused. */
        NO_WRITE_ACCESS                 = -2086
        NO_DELETE_ACCESS                = -2087
        CDF_DELETE_ERROR                = -2088
        VAR_DELETE_ERROR                = -2089
        UNKNOWN_COMPRESSION             = -2090
        CANNOT_COMPRESS                 = -2091
        DECOMPRESSION_ERROR             = -2092
        COMPRESSION_ERROR               = -2093
#                     /* -2094 unused. */
#                     /* -2095 unused. */
        EMPTY_COMPRESSED_CDF            = -2096
        BAD_COMPRESSION_PARM            = -2097
        UNKNOWN_SPARSENESS              = -2098
        CANNOT_SPARSERECORDS            = -2099
        CANNOT_SPARSEARRAYS             = -2100
        TOO_MANY_PARMS                  = -2101
        NO_SUCH_RECORD                  = -2102
        CANNOT_ALLOCATE_RECORDS         = -2103
#                     /* -2104 unused. */
#                     /* -2105 unused. */
        SCRATCH_DELETE_ERROR            = -2106
        SCRATCH_CREATE_ERROR            = -2107
        SCRATCH_READ_ERROR              = -2108
        SCRATCH_WRITE_ERROR             = -2109
        BAD_SPARSEARRAYS_PARM           = -2110
        BAD_SCRATCH_DIR                 = -2111
        NOT_A_CDF_OR_NOT_SUPPORTED      = -2113
        CORRUPTED_V3_CDF                = -2223
        ILLEGAL_EPOCH_FIELD             = -2224
        BAD_CHECKSUM                    = -2225
        CHECKSUM_ERROR                  = -2226
        CHECKSUM_NOT_ALLOWED            = -2227
        IS_A_NETCDF                     = -2228
        TT2000_TIME_ERROR               = -2229
        UNABLE_TO_PROCESS_CDF           = -2230
# /* #define ZLIB_DECOMPRESSION_ERROR        = -2231 */

# """
        # * Functions (for INTERNAL interface).
        # * Note: These values must be different from those of the items.
        CREATE_                 = 1001
        OPEN_                   = 1002
        DELETE_                 = 1003
        CLOSE_                  = 1004
        SELECT_                 = 1005
        CONFIRM_                = 1006
        GET_                    = 1007
        PUT_                    = 1008

        SAVE_                   = 1009
        BACKWARD_               = 1010
        GETCDFFILEBACKWARD_     = 1011
        CHECKSUM_               = 1012
        GETCDFCHECKSUM_         = 1013
        VALIDATE_               = 1014
        GETCDFVALIDATE_         = 1015
        GETLEAPSECONDSENVVAR_   = 1016

        NULL_                   = 1000

        # * Items on which functions are performed (for INTERNAL interface).
        # * Note: These values must be different from those of the functions.
        CDF_                    = 1
        CDF_NAME_               = 2
        CDF_ENCODING_           = 3
        CDF_DECODING_           = 4
        CDF_MAJORITY_           = 5
        CDF_FORMAT_             = 6
        CDF_COPYRIGHT_          = 7
        CDF_NUMrVARS_           = 8
        CDF_NUMzVARS_           = 9
        CDF_NUMATTRS_           = 10
        CDF_NUMgATTRS_          = 11
        CDF_NUMvATTRS_          = 12
        CDF_VERSION_            = 13
        CDF_RELEASE_            = 14
        CDF_INCREMENT_          = 15
        CDF_STATUS_             = 16
        CDF_READONLY_MODE_      = 17
        CDF_zMODE_              = 18
        CDF_NEGtoPOSfp0_MODE_   = 19
        LIB_COPYRIGHT_          = 20
        LIB_VERSION_            = 21
        LIB_RELEASE_            = 22
        LIB_INCREMENT_          = 23
        LIB_subINCREMENT_       = 24
        rVARs_NUMDIMS_          = 25
        rVARs_DIMSIZES_         = 26
        rVARs_MAXREC_           = 27
        rVARs_RECDATA_          = 28
        rVARs_RECNUMBER_        = 29
        rVARs_RECCOUNT_         = 30
        rVARs_RECINTERVAL_      = 31
        rVARs_DIMINDICES_       = 32
        rVARs_DIMCOUNTS_        = 33
        rVARs_DIMINTERVALS_     = 34
        rVAR_                   = 35
        rVAR_NAME_              = 36
        rVAR_DATATYPE_          = 37
        rVAR_NUMELEMS_          = 38
        rVAR_RECVARY_           = 39
        rVAR_DIMVARYS_          = 40
        rVAR_NUMBER_            = 41
        rVAR_DATA_              = 42
        rVAR_HYPERDATA_         = 43
        rVAR_SEQDATA_           = 44
        rVAR_SEQPOS_            = 45
        rVAR_MAXREC_            = 46
        rVAR_MAXallocREC_       = 47
        rVAR_DATASPEC_          = 48
        rVAR_PADVALUE_          = 49
        rVAR_INITIALRECS_       = 50
        rVAR_BLOCKINGFACTOR_    = 51
        rVAR_nINDEXRECORDS_     = 52
        rVAR_nINDEXENTRIES_     = 53
        rVAR_EXISTENCE_         = 54
        zVARs_MAXREC_           = 55
        zVARs_RECDATA_          = 56
        zVAR_                   = 57
        zVAR_NAME_              = 58
        zVAR_DATATYPE_          = 59
        zVAR_NUMELEMS_          = 60
        zVAR_NUMDIMS_           = 61
        zVAR_DIMSIZES_          = 62
        zVAR_RECVARY_           = 63
        zVAR_DIMVARYS_          = 64
        zVAR_NUMBER_            = 65
        zVAR_DATA_              = 66
        zVAR_HYPERDATA_         = 67
        zVAR_SEQDATA_           = 68
        zVAR_SEQPOS_            = 69
        zVAR_MAXREC_            = 70
        zVAR_MAXallocREC_       = 71
        zVAR_DATASPEC_          = 72
        zVAR_PADVALUE_          = 73
        zVAR_INITIALRECS_       = 74
        zVAR_BLOCKINGFACTOR_    = 75
        zVAR_nINDEXRECORDS_     = 76
        zVAR_nINDEXENTRIES_     = 77
        zVAR_EXISTENCE_         = 78
        zVAR_RECNUMBER_         = 79
        zVAR_RECCOUNT_          = 80
        zVAR_RECINTERVAL_       = 81
        zVAR_DIMINDICES_        = 82
        zVAR_DIMCOUNTS_         = 83
        zVAR_DIMINTERVALS_      = 84
        ATTR_                   = 85
        ATTR_SCOPE_             = 86
        ATTR_NAME_              = 87
        ATTR_NUMBER_            = 88
        ATTR_MAXgENTRY_         = 89
        ATTR_NUMgENTRIES_       = 90
        ATTR_MAXrENTRY_         = 91
        ATTR_NUMrENTRIES_       = 92
        ATTR_MAXzENTRY_         = 93
        ATTR_NUMzENTRIES_       = 94
        ATTR_EXISTENCE_         = 95
        gENTRY_                 = 96
        gENTRY_EXISTENCE_       = 97
        gENTRY_DATATYPE_        = 98
        gENTRY_NUMELEMS_        = 99
        gENTRY_DATASPEC_        = 100
        gENTRY_DATA_            = 101
        rENTRY_                 = 102
        rENTRY_NAME_            = 103
        rENTRY_EXISTENCE_       = 104
        rENTRY_DATATYPE_        = 105
        rENTRY_NUMELEMS_        = 106
        rENTRY_DATASPEC_        = 107
        rENTRY_DATA_            = 108
        zENTRY_                 = 109
        zENTRY_NAME_            = 110
        zENTRY_EXISTENCE_       = 111
        zENTRY_DATATYPE_        = 112
        zENTRY_NUMELEMS_        = 113
        zENTRY_DATASPEC_        = 114
        zENTRY_DATA_            = 115
        STATUS_TEXT_            = 116
        CDF_CACHESIZE_          = 117
        rVARs_CACHESIZE_        = 118
        zVARs_CACHESIZE_        = 119
        rVAR_CACHESIZE_         = 120
        zVAR_CACHESIZE_         = 121
        zVARs_RECNUMBER_        = 122
        rVAR_ALLOCATERECS_      = 123
        zVAR_ALLOCATERECS_      = 124
        DATATYPE_SIZE_          = 125
        CURgENTRY_EXISTENCE_    = 126
        CURrENTRY_EXISTENCE_    = 127
        CURzENTRY_EXISTENCE_    = 128
        CDF_INFO_               = 129
        CDF_COMPRESSION_        = 130
        zVAR_COMPRESSION_       = 131
        zVAR_SPARSERECORDS_     = 132
        zVAR_SPARSEARRAYS_      = 133
        zVAR_ALLOCATEBLOCK_     = 134
        zVAR_NUMRECS_           = 135
        zVAR_NUMallocRECS_      = 136
        rVAR_COMPRESSION_       = 137
        rVAR_SPARSERECORDS_     = 138
        rVAR_SPARSEARRAYS_      = 139
        rVAR_ALLOCATEBLOCK_     = 140
        rVAR_NUMRECS_           = 141
        rVAR_NUMallocRECS_      = 142
        rVAR_ALLOCATEDFROM_     = 143
        rVAR_ALLOCATEDTO_       = 144
        zVAR_ALLOCATEDFROM_     = 145
        zVAR_ALLOCATEDTO_       = 146
        zVAR_nINDEXLEVELS_      = 147
        rVAR_nINDEXLEVELS_      = 148
        CDF_SCRATCHDIR_         = 149
        rVAR_RESERVEPERCENT_    = 150
        zVAR_RESERVEPERCENT_    = 151
        rVAR_RECORDS_           = 152
        zVAR_RECORDS_           = 153
        STAGE_CACHESIZE_        = 154
        COMPRESS_CACHESIZE_     = 155
        CDF_CHECKSUM_           = 156

        CDFwithSTATS_           = 200    # /* For CDF internal use only! */
        CDF_ACCESS_             = 201    # /* For CDF internal use only! */

    #End of enum

    double TT2000END = -99999.999


    CDFstatus CDFlib(long op1, ...)


#----------------- numpy stuff ----------------------------
cimport numpy as numpy_c
cdef extern from "numpy/arrayobject.h":
    object PyArray_NewFromDescr(object subtype, 
                                numpy_c.dtype descr,
                                int nd, 
                                numpy_c.npy_intp* dims, 
                                numpy_c.npy_intp* strides,
                                void* data, 
                                int flags, 
                                object obj)
    enum:
        NPY_C_CONTIGUOUS
        NPY_F_CONTIGUOUS
        NPY_CONTIGUOUS
        NPY_FORTRAN
        NPY_OWNDATA
        NPY_FORCECAST
        NPY_ENSURECOPY
        NPY_ENSUREARRAY
        NPY_ELEMENTSTRIDES
        NPY_ALIGNED
        NPY_NOTSWAPPED
        NPY_WRITEABLE
        NPY_UPDATEIFCOPY
        NPY_ARR_HAS_DESCR

        NPY_BEHAVED
        NPY_BEHAVED_NS
        NPY_CARRAY
        NPY_CARRAY_RO
        NPY_FARRAY
        NPY_FARRAY_RO
        NPY_DEFAULT

        NPY_IN_ARRAY
        NPY_OUT_ARRAY
        NPY_INOUT_ARRAY
        NPY_IN_FARRAY
        NPY_OUT_FARRAY
        NPY_INOUT_FARRAY

        NPY_UPDATE_ALL


# 
